//
//  MSRFinancialRoomSettlementCell.m
//  觅尚
//
//  Created by No.8 on 2019/4/7.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSBindingRelationsCell.h"

@implementation MSBindingRelationsCell
- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self CreatView];
    }
    return self;
}
- (void)CreatView{
    
    /** 背景*/
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(20 * px, 0, ScreenWidth / 2 - 40 * px , 680 * px)];
    self.backView.layer.cornerRadius = 5;
    self.backView.layer.masksToBounds = YES;
    self.backView.layer.borderWidth = 1;
    self.backView.layer.borderColor = WYCRGBColor(75, 60, 53).CGColor;
    [self addSubview:self.backView];
    
    
    self.RoomBackView = [[UIView alloc] initWithFrame:CGRectMake(20 * px, 40 * px, self.backView.width - 40 * px, 80 * px)];
    self.RoomBackView.backgroundColor = WYCRGBColor(69, 55, 63);
    self.RoomBackView.cornerRadius = 40 * px;
    [self.backView addSubview:self.RoomBackView];
    
    
    /** 开房状态*/
    self.StateIMG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80 * px, 80 * px)];
    self.StateIMG.layer.cornerRadius = 40 * px;
    self.StateIMG.layer.masksToBounds = YES;
    self.StateIMG.image = [UIImage imageNamed:@"chaoxiaofei"];
    [self.RoomBackView addSubview:self.StateIMG];
    
    
    //房间号
    self.RoomnumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.StateIMG.frame) + 10 * px, self.StateIMG.y, 200 * px, self.StateIMG.height)];
    self.RoomnumberLabel.text = @"301";
    self.RoomnumberLabel.textColor = [UIColor orangeColor];
    self.RoomnumberLabel.textAlignment = NSTextAlignmentLeft;
    self.RoomnumberLabel.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
    [self.RoomBackView addSubview:self.RoomnumberLabel];
    
    
    //房间价格
    self.RoomPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.RoomBackView.width - 300 * px, self.StateIMG.y, 280 * px, self.StateIMG.height)];
    self.RoomPriceLabel.text = @"￥800";
    self.RoomPriceLabel.textColor = [UIColor whiteColor];
    self.RoomPriceLabel.textAlignment = NSTextAlignmentRight;
    self.RoomPriceLabel.font = [UIFont systemFontOfSize:SYRealValue(26 / 2)];
    [self.RoomBackView addSubview:self.RoomPriceLabel];
    
    
    
    //房间消费
    self.CurrentConsumptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.RoomBackView.x, CGRectGetMaxY(self.RoomBackView.frame) + 40 * px, self.RoomBackView.width, 60 * px)];
//    self.CurrentConsumptionLabel.text = @"当前消费：￥2000";
    self.CurrentConsumptionLabel.textColor = [UIColor redColor];
    self.CurrentConsumptionLabel.textAlignment = NSTextAlignmentLeft;
    self.CurrentConsumptionLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    [self.backView addSubview:self.CurrentConsumptionLabel];
    
    
    /********************开房人********/
    //开房人
    self.HouseownerTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.CurrentConsumptionLabel.x, CGRectGetMaxY(self.CurrentConsumptionLabel.frame) + 40 * px, 150 * px, 60 * px)];
    self.HouseownerTitleLabel.text = @"开房人:";
    self.HouseownerTitleLabel.textColor = self.RoomPriceLabel.textColor;
    self.HouseownerTitleLabel.textAlignment = NSTextAlignmentLeft;
    self.HouseownerTitleLabel.font = [UIFont systemFontOfSize:SYRealValue(26 / 2)];
    [self.backView addSubview:self.HouseownerTitleLabel];
    
    
    
    /** 开房人头像*/
    self.HouseownerIMG = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.HouseownerTitleLabel.frame) + 10 * px, self.HouseownerTitleLabel.y, 60 * px, 60 * px)];
    self.HouseownerIMG.layer.cornerRadius = 30 * px;
    self.HouseownerIMG.layer.masksToBounds = YES;
    self.HouseownerIMG.backgroundColor = [UIColor clearColor];
//    self.HouseownerIMG.image = [UIImage imageNamed:@"chaoxiaofei"];
    [self.backView addSubview:self.HouseownerIMG];
    
    
    /** 开房人头像*/
    self.HouseownerLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.HouseownerIMG.frame) + 10 * px, self.HouseownerTitleLabel.y, 200 * px, 60 * px)];
//    self.HouseownerLabel.text = @"张三";
    self.HouseownerLabel.textColor = [UIColor grayColor];
    self.HouseownerLabel.font = [UIFont systemFontOfSize:SYRealValue(26 / 2)];
    [self.backView addSubview:self.HouseownerLabel];
    
    
    /********************服务生********/
    
    //服务生
    self.WaiterTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.CurrentConsumptionLabel.x, CGRectGetMaxY(self.HouseownerTitleLabel.frame) + 40 * px, self.HouseownerTitleLabel.width, self.HouseownerTitleLabel.height)];
    self.WaiterTitleLabel.text = @"服务生:";
    self.WaiterTitleLabel.textColor = self.RoomPriceLabel.textColor;
    self.WaiterTitleLabel.textAlignment = NSTextAlignmentLeft;
    self.WaiterTitleLabel.font = [UIFont systemFontOfSize:SYRealValue(26 / 2)];
    [self.backView addSubview:self.WaiterTitleLabel];
    
    
    
    /** 开房人头像*/
    self.WaiterIMG = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.WaiterTitleLabel.frame) + 10 * px, self.WaiterTitleLabel.y, self.HouseownerIMG.width, self.HouseownerIMG.height)];
    self.WaiterIMG.layer.cornerRadius = 30 * px;
    self.WaiterIMG.layer.masksToBounds = YES;
//    self.WaiterIMG.image = [UIImage imageNamed:@"chaoxiaofei"];
    self.WaiterIMG.backgroundColor = [UIColor clearColor];
    [self.backView addSubview:self.WaiterIMG];
    
    
    /** 开房人姓名*/
    self.WaiterLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.WaiterIMG.frame) + 10 * px, self.WaiterTitleLabel.y, self.HouseownerLabel.width, self.HouseownerLabel.height)];
//    self.HouseownerLabel.text = @"张三";
    self.WaiterLabel.textColor = [UIColor grayColor];
    self.WaiterLabel.font = [UIFont systemFontOfSize:SYRealValue(26 / 2)];
    [self.backView addSubview:self.WaiterLabel];
    
    
    
    /********************公关********/
    
    //公关
    self.RelationsTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.CurrentConsumptionLabel.x, CGRectGetMaxY(self.WaiterTitleLabel.frame) + 40 * px, self.HouseownerTitleLabel.width, self.HouseownerTitleLabel.height)];
    self.RelationsTitleLabel.text = @"公   关:";
    self.RelationsTitleLabel.textColor = self.RoomPriceLabel.textColor;
    self.RelationsTitleLabel.textAlignment = NSTextAlignmentLeft;
    self.RelationsTitleLabel.font = [UIFont systemFontOfSize:SYRealValue(26 / 2)];
    [self.backView addSubview:self.RelationsTitleLabel];
    
    self.RelationsView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.RelationsTitleLabel.frame) + 10 * px, self.RelationsTitleLabel.y, self.backView.width - self.RelationsTitleLabel.width - 40 * px, (self.backView.height - self.RelationsTitleLabel.y) - 100 * px)];
    self.RelationsView.backgroundColor = [UIColor clearColor];
    [self.backView addSubview:self.RelationsView];
    
    int totalColumns = 2;
    //每行列数
    
    CGFloat W = 160 * px;
    CGFloat H = 80 * px;
    
    //    间隙
    CGFloat margin = (self.RelationsView.width - totalColumns * W) / (totalColumns + 1);
    for (int i = 0; i < 4; i ++) {
        //创建按钮
        self.HeadNameButton = [[MSHeadnameButton alloc] init];
        
        /** 公关头像姓名*/
        
        int row = i / totalColumns;
        int col = i % totalColumns;
        //根据行号和列号来确定 子控件的坐标
        CGFloat cellX = margin + col * (W + margin);
        CGFloat cellY = row * (H + margin);
        
        self.HeadNameButton = [[MSHeadnameButton alloc] initWithFrame:CGRectMake(cellX, cellY, W, H)];
        self.HeadNameButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(22 / 2)];
        [self.HeadNameButton  setTitleColor:self.HouseownerLabel.textColor forState:UIControlStateNormal];
        self.HeadNameButton.imageView.layer.cornerRadius = 40 * px;
        self.HeadNameButton.imageView.layer.masksToBounds = YES;
//        [self.HeadNameButton setTitle:@"11111" forState:UIControlStateNormal];
//        self.HeadNameButton.backgroundColor = [UIColor blackColor];
        [self.RelationsView  addSubview:self.HeadNameButton];
        self.HeadNameButton.hidden = YES;
//        self.RelationsView.hidden = YES;
    }
    
    /** 结算*/
    self.SettlementButton = [[UIButton alloc] initWithFrame:CGRectMake(20 * px, CGRectGetMaxY(self.RelationsView.frame), self.backView.width / 2 - 40 * px , 60 * px)];
//    [self.SettlementButton setTitle:@"绑定公关" forState:UIControlStateNormal];
    self.SettlementButton.layer.cornerRadius = 30 * px;
    self.SettlementButton.layer.masksToBounds = YES;
    [self.SettlementButton addTarget:self action:@selector(SettlementButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.SettlementButton setBackgroundImage:[UIImage imageNamed:@"底部按钮背景"] forState:UIControlStateNormal];
    self.SettlementButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    [self.SettlementButton  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.backView  addSubview:self.SettlementButton];
    
    
    
    /** 结算*/
    self.OpenRoomButton = [[UIButton alloc] initWithFrame:CGRectMake(self.backView.width / 2 + 20 * px, CGRectGetMaxY(self.RelationsView.frame), self.SettlementButton.width, self.SettlementButton.height)];
//    [self.OpenRoomButton setTitle:@"开房" forState:UIControlStateNormal];
    self.OpenRoomButton.layer.cornerRadius = 30 * px;
    self.OpenRoomButton.layer.masksToBounds = YES;
    [self.OpenRoomButton addTarget:self action:@selector(OpenRoomButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.OpenRoomButton setBackgroundImage:[UIImage imageNamed:@"底部按钮背景"] forState:UIControlStateNormal];
    self.OpenRoomButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    [self.OpenRoomButton  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.backView  addSubview:self.OpenRoomButton];
    
    
    

    
}
- (void)OpenRoomButtonClick{
    WYCLog(@"avatarUrl================%@",self.RoomManageMentModel.openid);
    _HouseOwnerModel = [MSHouseOwnerModel mj_objectWithKeyValues:self.RoomManageMentModel.houseOwner];
    if (self.SettlementButtonDelegate && [self.SettlementButtonDelegate respondsToSelector:@selector(ClickSettlementButtonUid:Roomid:RoomName:RoomMoney:HourIMG:HourName:Hourid:WaiterIMG:WaiterName:WaiterID:openid:)]) {
        [self.SettlementButtonDelegate ClickSettlementButtonUid:self.RoomManageMentModel.runid Roomid:self.RoomManageMentModel.roomId RoomName:self.RoomManageMentModel.roomName RoomMoney:self.RoomManageMentModel.money HourIMG:_HouseOwnerModel.avatarUrl HourName:_HouseOwnerModel.userName Hourid:_HouseOwnerModel.uid WaiterIMG:self.RoomManageMentModel.WaiterModel.avatarUrl WaiterName:self.RoomManageMentModel.WaiterModel.userName WaiterID:self.RoomManageMentModel.WaiterModel.uid openid:self.RoomManageMentModel.openid];
    }
    
}
- (void)SettlementButtonClick{
    _HouseOwnerModel = [MSHouseOwnerModel mj_objectWithKeyValues:self.RoomManageMentModel.houseOwner];
    if (self.SettlementButtonDelegate && [self.SettlementButtonDelegate respondsToSelector:@selector(ClickSettlementButtonUid:Roomid:RoomName:RoomMoney:HourIMG:HourName:Hourid:WaiterIMG:WaiterName:WaiterID:openid:)]) {
        [self.SettlementButtonDelegate ClickSettlementButtonUid:self.RoomManageMentModel.runid Roomid:self.RoomManageMentModel.roomId RoomName:self.RoomManageMentModel.roomName RoomMoney:self.RoomManageMentModel.money HourIMG:_HouseOwnerModel.avatarUrl HourName:_HouseOwnerModel.userName Hourid:_HouseOwnerModel.uid WaiterIMG:self.RoomManageMentModel.WaiterModel.avatarUrl WaiterName:self.RoomManageMentModel.WaiterModel.userName WaiterID:self.RoomManageMentModel.WaiterModel.uid openid:self.RoomManageMentModel.openid];
    }
    
}
- (void)setRoomManageMentModel:(MSRoomManageMentModel *)RoomManageMentModel{
//    _MyModel = MyModel;
//    [RoomManageMentModel.princessess removeAllObjects];
    _RoomManageMentModel = RoomManageMentModel;
    _HouseOwnerModel = [MSHouseOwnerModel mj_objectWithKeyValues:RoomManageMentModel.houseOwner];
//    _WaiterModel = [MSWaiterModel mj_objectWithKeyValues:RoomManageMentModel.waiter];
//    WYCLog(@"------%@",_WaiterModel.userName);
//
    
    
    self.RoomnumberLabel.text = [NSString stringWithFormat:@"%@",RoomManageMentModel.roomName];
    
    
    //房间价格
    self.RoomPriceLabel.text = [NSString stringWithFormat:@"%@",RoomManageMentModel.money];
    if (RoomManageMentModel.sumMoney == nil || RoomManageMentModel.sumMoney == NULL || [RoomManageMentModel.sumMoney isKindOfClass:[NSNull class]] || [[RoomManageMentModel.sumMoney stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0){
       self.CurrentConsumptionLabel.text = @"当前消费:￥0";
    }else{
        self.CurrentConsumptionLabel.text = [NSString stringWithFormat:@"当前消费:￥ %@",RoomManageMentModel.sumMoney];
        
    }
    //房间消费
//
    
    if (_HouseOwnerModel.userName == nil || _HouseOwnerModel.userName == NULL || [_HouseOwnerModel.userName isKindOfClass:[NSNull class]] || [[_HouseOwnerModel.userName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0){
        self.HouseownerLabel.text = @"";
    }else{
        self.HouseownerLabel.text = [NSString stringWithFormat:@"%@",_HouseOwnerModel.userName];
    
    }
    if (_HouseOwnerModel.avatarUrl == nil || _HouseOwnerModel.avatarUrl == NULL || [_HouseOwnerModel.avatarUrl isKindOfClass:[NSNull class]] || [[_HouseOwnerModel.avatarUrl stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0){
        self.HouseownerIMG.image = [UIImage imageNamed:@""];
    }else{
        
        [self.HouseownerIMG sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",_HouseOwnerModel.avatarUrl]]];
        
    }
    
    
    
//    WYCLog(@"=========%@",RoomManageMentModel.WaiterModel.userName);
    if (RoomManageMentModel.WaiterModel.userName == nil || RoomManageMentModel.WaiterModel.userName == NULL || [RoomManageMentModel.WaiterModel.userName isKindOfClass:[NSNull class]] || [[RoomManageMentModel.WaiterModel.userName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0){
        self.WaiterLabel.text = @"";
    }else{
        
        self.WaiterLabel.text = [NSString stringWithFormat:@"%@",RoomManageMentModel.WaiterModel.userName];
        
    }
    if (RoomManageMentModel.WaiterModel.avatarUrl == nil || RoomManageMentModel.WaiterModel.avatarUrl == NULL || [RoomManageMentModel.WaiterModel.avatarUrl isKindOfClass:[NSNull class]] || [[RoomManageMentModel.WaiterModel.avatarUrl stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) {
        self.WaiterIMG.image = [UIImage imageNamed:@""];
    }else{
        
        [self.WaiterIMG sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",RoomManageMentModel.WaiterModel.avatarUrl]]];
    }
    
    
    [self.RelationsView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.hidden = YES;
    }];
    
    for (int i = 0; i < self.RelationsView.subviews.count; i++) {
        WYCLog(@"-----------%lu",(unsigned long)RoomManageMentModel.princessess.count);
        if (i >= RoomManageMentModel.princessess.count) {
            break;
        }

        self.PrincessessModel = RoomManageMentModel.princessess[i];
        self.HeadNameButton = self.RelationsView.subviews[i];
//        self.RelationsView.hidden = NO;
        self.HeadNameButton.hidden = NO;
        if (RoomManageMentModel.princessess == nil || RoomManageMentModel.princessess == NULL || [RoomManageMentModel.princessess isKindOfClass:[NSNull class]]) {
            [self.RelationsView removeFromSuperview];
           
        }
            [self.HeadNameButton sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.PrincessessModel.avatarUrl]] forState:UIControlStateNormal];

            [self.HeadNameButton setTitle:self.PrincessessModel.userName forState:UIControlStateNormal];
    }
    
}


//- (void)setMyModel:(MSMyModel *)MyModel{
//    
//    
//   
//    if ([_RoomManageMentModel.roomState isEqualToString:@"0"]) {
//                self.StateIMG.image = [UIImage imageNamed:@"weishiyong"];
//                self.RoomBackView.backgroundColor = BackViewColor;
//                self.backView.layer.borderColor = BackViewColor.CGColor;
//        if ([MyModel.key isEqualToString:@"room_open"]) {
//            
//            [self.SettlementButton setTitle:@"开房" forState:UIControlStateNormal];
////            [self.OpenRoomButton setTitle:@"开房" forState:UIControlStateNormal];
//        }else if ([MyModel.key isEqualToString:@"room_reserve"]){
//            [self.SettlementButton setTitle:@"开房" forState:UIControlStateNormal];
//        }
//        
//        
//        
//        }
//        else if ([_RoomManageMentModel.roomState isEqualToString:@"1"]) {
//            self.StateIMG.image = [UIImage imageNamed:@"kaifang"];
//            self.RoomBackView.backgroundColor = WYCRGBColor(54, 55, 96);
//            self.backView.layer.borderColor = WYCRGBColor(54, 55, 96).CGColor;
//            [self.SettlementButton setTitle:@"绑定公关" forState:UIControlStateNormal];
//            [self.OpenRoomButton setTitle:@"结算" forState:UIControlStateNormal];
//        }else if ([_RoomManageMentModel.roomState isEqualToString:@"2"]){
//            self.StateIMG.image = [UIImage imageNamed:@"yuding"];
//            self.RoomBackView.backgroundColor = WYCRGBColor(39, 77, 90);
//            self.backView.layer.borderColor = WYCRGBColor(39, 77, 90).CGColor;
//            [self.SettlementButton setTitle:@"开房" forState:UIControlStateNormal];
//            [self.OpenRoomButton setTitle:@"退房" forState:UIControlStateNormal];
//        }else{
//            self.StateIMG.image = [UIImage imageNamed:@"chaoxiaofei"];
//            self.RoomBackView.backgroundColor = WYCRGBColor(69, 55, 63);
//            self.backView.layer.borderColor = WYCRGBColor(75, 60, 53).CGColor;
//            [self.SettlementButton setTitle:@"换房" forState:UIControlStateNormal];
//            [self.OpenRoomButton setTitle:@"结算" forState:UIControlStateNormal];
//        }
//    
//}
@end
